import 'package:ls_work/util/ext_utils.dart';
import 'package:pinyin/pinyin.dart';

class AddressBookEntity {
  final int? total;
  List<AddressBookSection>? rows;

  AddressBookEntity({this.rows, this.total});

  factory AddressBookEntity.fromJson(Map<String, dynamic> json) {
    final originList = json['rows'] as List?;
    List<AddressBookSection> protocolList =
        (originList??[]).map((value) => AddressBookSection.fromJson(value)).toList();
    return AddressBookEntity(rows: protocolList, total: json['total']);
  }
}

class AddressBookSection {
  String? officeName;
  List<AddressBookInfo>? staffPhoneBooks;

  AddressBookSection({
    this.officeName,
    this.staffPhoneBooks,
  });

  factory AddressBookSection.fromJson(Map<String, dynamic> json) {
    final originList = json['staffPhoneBooks'] as List?;
    List<AddressBookInfo> protocolList =
        (originList??[]).map((value) => AddressBookInfo.fromJson(value)).toList();
    return AddressBookSection(
      staffPhoneBooks: protocolList,
      officeName: json['officeName'],
    );
  }
}

class AddressBookInfo {
  String? name;
  String? mobile;
  String? firstPy;
  String? shortPy;
  String? officeName;

  String get nickName {
    if((name??"").isChinese){
      if(name!.length<=2){
        return name??"";
      }else{
        return name!.substring(1,3);
      }
    }else{
      if((name??"").length<=2){
        return name??"";
      }else{
        return name!.substring(0,2);
      }
    }
  }


  AddressBookInfo({
    this.name,
    this.mobile,
    this.officeName
  });

  AddressBookInfo.fromJson(Map data) {
    name = data['name'];
    mobile = data['mobile'];
    officeName = data['officeName'];
    if((name??"").isChinese){
      firstPy = PinyinHelper.getFirstWordPinyin(name!).substring(0,1).toUpperCase();
      shortPy = PinyinHelper.getShortPinyin(name!).toUpperCase();
    }else{
      if(name==null||name!.isEmpty){
        firstPy = "#".toUpperCase();
        shortPy = "#".toUpperCase();
      }else{
        firstPy = name!.substring(0,1).toUpperCase();
        shortPy = name!.toUpperCase();
      }
    }

  }
}
